package com.ricchieri.manage.bean;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.ricchieri.filter.EmailToSendFilter;
import com.ricchieri.lazymodel.EmailToSendLazyDataModel;
import com.ricchieri.model.EmailToSend;
import com.ricchieri.model.EmailToSendType;
import com.ricchieri.service.interf.IDocumentTypeService;
import com.ricchieri.service.interf.IEmailToSendService;
import com.ricchieri.service.interf.IEmailToSendTypeService;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "viewEmailToSendMB")
@ViewScoped
public class ViewEmailsStatusManageBean extends GenericManageBean {

	private static final long serialVersionUID = -3624916374734996164L;
	
	@ManagedProperty(value = "#{DocumentTypeService}")
	IDocumentTypeService documentTypeService;
	
	@ManagedProperty(value = "#{EmailToSendTypeService}")
	IEmailToSendTypeService emailTypeService;	
	
	@ManagedProperty(value = "#{EmailToSendService}")
	IEmailToSendService emailToSendService;		

	private EmailToSendFilter emailToSendFilter = new EmailToSendFilter();
	
	private Map<String, String> documentType;
	private Map<String,String> emailToSendType;
	private EmailToSendLazyDataModel emailToSendLazyDataModel;
	
	public Map<String, String> getEmailToSendType() {
		if (emailToSendType == null){
			emailToSendType = new TreeMap<String, String>();
			List<EmailToSendType> template = emailTypeService.getAllEmailToSendType();
			for (EmailToSendType tem : template) {
				emailToSendType.put(FacesUtil.getMessage(tem.getDescription()), String.valueOf(tem.getId()));
			}
		}
		return emailToSendType;
	}

	public IEmailToSendTypeService getEmailTypeService() {
		return emailTypeService;
	}

	public void setEmailTypeService(IEmailToSendTypeService emailTypeService) {
		this.emailTypeService = emailTypeService;
	}
	
	public IDocumentTypeService getDocumentTypeService() {
		return documentTypeService;
	}
	
	public void setDocumentTypeService(IDocumentTypeService documentTypeService) {
		this.documentTypeService = documentTypeService;
	}
	
	public Map<String, String> getDocumentType() {
		if(documentType == null) {
			documentType = getDocumentTypeService().getComboDocuments();
		}
		return documentType;
	}
	
	public void setDocumentType(Map<String, String> documentType) {
		this.documentType = documentType;
	}
	
	public void search() {
		emailToSendLazyDataModel = null;
	}
	
	public void reset() {
		emailToSendLazyDataModel = null;
		emailToSendFilter = new EmailToSendFilter();
	}

	public EmailToSendLazyDataModel getEmailToSendLazyDataModel() {
		if(emailToSendLazyDataModel == null) {
			emailToSendFilter.setFindIsMessageSent(Boolean.FALSE);
			emailToSendLazyDataModel = new EmailToSendLazyDataModel(emailToSendService, emailToSendFilter);
		}
		return emailToSendLazyDataModel;
	}

	public void setEmailToSendLazyDataModel(EmailToSendLazyDataModel emailToSendLazyDataModel) {
		this.emailToSendLazyDataModel = emailToSendLazyDataModel;
	}

	public EmailToSendFilter getEmailToSendFilter() {
		return emailToSendFilter;
	}

	public void setEmailToSendFilter(EmailToSendFilter emailToSendFilter) {
		this.emailToSendFilter = emailToSendFilter;
	}

	public IEmailToSendService getEmailToSendService() {
		return emailToSendService;
	}

	public void setEmailToSendService(IEmailToSendService emailToSendService) {
		this.emailToSendService = emailToSendService;
	}

	public void setEmailToSendType(Map<String, String> emailToSendType) {
		this.emailToSendType = emailToSendType;
	}
	
	public void resendEmail(EmailToSend emailToSend){
		emailToSend.setAttemptCounter(0);
		emailToSend.setSendDate(null);
		emailToSend.setIsMessageSent(Boolean.FALSE);
		
		getEmailToSendService().update(emailToSend);
		
		FacesUtil.showInfoMessage("viewEmailStatus", "emailToSendMsgSuccess");
	}
}

